package com.my.controller;

import com.my.UserInfo;
import com.my.dao.UserInfoModel;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UserInfoControl", value = "/userInfoControl")
public class UserInfoControl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        // 通过请求参数获取用户输入的信息：用户名、密码、地址信息
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setPassword(password);
        userInfo.setAddress(address);
        UserInfoModel model = new UserInfoModel();
        boolean result = model.insert(userInfo);
        String path = "";
        if (result) { // 保存成功
            path = "/userInfoSuccess.jsp";
        } else { // 保存失败
            path = "/userInfoError.jsp";
        }
        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);
    }
}